"""
服务执行器
根据服务协调器的选择执行相应的服务
"""

import inspect
import asyncio
import time
import random
import logging
from collections import defaultdict
from typing import Dict, Any, Optional, List
from src.services.service_registry import ServiceRegistry, service_registry, ServiceStatus
from datetime import datetime
from src.utils.async_utils import resolve_awaitable, run_in_async_context, AsyncHelper


class LoadBalancer:
    """智能负载均衡器，支持多种负载均衡算法"""
    
    def __init__(self, algorithm: str = "adaptive"):
        self.algorithm = algorithm  # 默认使用自适应算法
        self.service_instances: Dict[str, List[Dict[str, Any]]] = {}
        self.last_selected: Dict[str, int] = {}  # 用于轮询算法
        self.current_weights: Dict[str, List[int]] = {}  # 用于权重轮询
        self.instance_stats: Dict[str, Dict[str, Any]] = {}  # 实例统计信息
        self.last_update_time = time.time()
        self.update_interval = 60  # 60秒更新一次统计信息
    
    def register_service_instances(self, service_name: str, instances: List[Dict[str, Any]]):
        """注册服务实例"""
        # 为每个实例初始化必要的字段
        for instance in instances:
            instance.setdefault("healthy", True)
            instance.setdefault("connections", 0)
            instance.setdefault("weight", 1)  # 默认权重为1
            instance.setdefault("response_time", 0.1)  # 默认响应时间
            instance.setdefault("error_rate", 0)  # 默认错误率
            instance.setdefault("instance_id", instance.get("url", str(id(instance))))
        
        self.service_instances[service_name] = instances
        self.last_selected[service_name] = -1
        
        # 初始化权重数组
        weights = [instance.get("weight", 1) for instance in instances]
        self.current_weights[service_name] = weights.copy()
        
        # 初始化实例统计信息
        if service_name not in self.instance_stats:
            self.instance_stats[service_name] = {}
        
        for instance in instances:
            instance_id = instance["instance_id"]
            if instance_id not in self.instance_stats[service_name]:
                self.instance_stats[service_name][instance_id] = {
                    "total_connections": 0,
                    "total_response_time": 0,
                    "error_count": 0,
                    "success_count": 0
                }
    
    def get_instance(self, service_name: str) -> Optional[Dict[str, Any]]:
        """根据负载均衡算法获取服务实例"""
        # 定期更新统计信息
        current_time = time.time()
        if current_time - self.last_update_time > self.update_interval:
            self._update_statistics()
            self.last_update_time = current_time
        
        instances = self.service_instances.get(service_name, [])
        if not instances:
            return None
            
        # 过滤掉不健康的服务实例
        healthy_instances = [inst for inst in instances if inst.get("healthy", True)]
        if not healthy_instances:
            return None
            
        # 根据当前算法选择实例
        if self.algorithm == "adaptive":
            # 自适应算法：根据系统状态自动选择最佳算法
            return self._adaptive(service_name, healthy_instances)
        elif self.algorithm == "round_robin":
            return self._round_robin(service_name, healthy_instances)
        elif self.algorithm == "weighted_round_robin":
            return self._weighted_round_robin(service_name, healthy_instances)
        elif self.algorithm == "random":
            return self._random(healthy_instances)
        elif self.algorithm == "least_connections":
            return self._least_connections(healthy_instances)
        elif self.algorithm == "response_time_aware":
            return self._response_time_aware(healthy_instances)
        elif self.algorithm == "activity_aware":
            return self._activity_aware(healthy_instances)
        else:
            # 默认使用轮询算法
            return self._round_robin(service_name, healthy_instances)
    
    def update_instance_status(self, service_name: str, instance_id: str, 
                              connections: int = None, response_time: float = None,
                              error: bool = False):
        """更新服务实例状态"""
        if service_name not in self.instance_stats:
            return
        
        stats = self.instance_stats[service_name].get(instance_id, {})
        if not stats:
            return
        
        # 更新连接数
        if connections is not None:
            for instance in self.service_instances.get(service_name, []):
                if instance.get("instance_id") == instance_id:
                    instance["connections"] = connections
                    break
        
        # 更新响应时间
        if response_time is not None:
            stats.setdefault("total_response_time", 0)
            stats.setdefault("response_count", 0)
            stats["total_response_time"] += response_time
            stats["response_count"] += 1
            
            # 计算平均响应时间
            avg_response_time = stats["total_response_time"] / stats["response_count"]
            for instance in self.service_instances.get(service_name, []):
                if instance.get("instance_id") == instance_id:
                    instance["response_time"] = avg_response_time
                    break
        
        # 更新错误计数
        if error:
            stats.setdefault("error_count", 0)
            stats["error_count"] += 1
            
            # 计算总请求数
            total_requests = stats.get("success_count", 0) + stats["error_count"]
            if total_requests > 0:
                error_rate = stats["error_count"] / total_requests
                for instance in self.service_instances.get(service_name, []):
                    if instance.get("instance_id") == instance_id:
                        instance["error_rate"] = error_rate
                        break
        else:
            stats.setdefault("success_count", 0)
            stats["success_count"] += 1
    
    def _update_statistics(self):
        """更新统计信息"""
        # 这个方法可以根据需要扩展，例如更新健康检查状态等
        pass
    
    def _adaptive(self, service_name: str, instances: List[Dict[str, Any]]) -> Dict[str, Any]:
        """自适应负载均衡算法"""
        # 计算实例的健康状况和性能指标
        instance_metrics = []
        
        for instance in instances:
            # 计算综合分数（0-100）
            health_score = 100 if instance.get("healthy", True) else 0
            
            # 连接数评分（连接数越少评分越高）
            connections = instance.get("connections", 0)
            conn_score = max(0, 100 - min(connections * 10, 100))
            
            # 响应时间评分（响应时间越短评分越高）
            response_time = instance.get("response_time", 1.0)
            resp_score = max(0, 100 - min(response_time * 50, 100))
            
            # 错误率评分（错误率越低评分越高）
            error_rate = instance.get("error_rate", 0)
            error_score = max(0, 100 - min(error_rate * 100, 100))
            
            # 综合评分（权重可以调整）
            overall_score = (health_score * 0.4 + conn_score * 0.2 + 
                           resp_score * 0.2 + error_score * 0.2)
            
            instance_metrics.append((instance, overall_score))
        
        # 按综合评分排序，选择评分最高的实例
        instance_metrics.sort(key=lambda x: x[1], reverse=True)
        
        # 考虑一定的随机性，避免总是选择同一个实例
        if len(instance_metrics) > 1:
            # 前两个实例有较高的选中概率
            if random.random() < 0.7:
                return instance_metrics[0][0]
            else:
                return instance_metrics[1][0]
        
        return instance_metrics[0][0]
    
    def _round_robin(self, service_name: str, instances: List[Dict[str, Any]]) -> Dict[str, Any]:
        """轮询算法"""
        last_index = self.last_selected.get(service_name, -1)
        next_index = (last_index + 1) % len(instances)
        self.last_selected[service_name] = next_index
        return instances[next_index]
    
    def _weighted_round_robin(self, service_name: str, instances: List[Dict[str, Any]]) -> Dict[str, Any]:
        """权重轮询算法"""
        # 初始化或更新权重数组
        if service_name not in self.current_weights or len(self.current_weights[service_name]) != len(instances):
            self.current_weights[service_name] = [instance.get("weight", 1) for instance in instances]
        
        # 查找当前权重最大的实例
        max_weight = max(self.current_weights[service_name])
        max_indices = [i for i, w in enumerate(self.current_weights[service_name]) if w == max_weight]
        
        # 随机选择一个权重最大的实例
        selected_index = random.choice(max_indices)
        
        # 减少所有实例的当前权重
        for i in range(len(self.current_weights[service_name])):
            if i == selected_index:
                # 对于选中的实例，减去总权重
                total_weight = sum(instance.get("weight", 1) for instance in instances)
                self.current_weights[service_name][i] -= total_weight
            # 加上初始权重
            self.current_weights[service_name][i] += instances[i].get("weight", 1)
        
        return instances[selected_index]
    
    def _random(self, instances: List[Dict[str, Any]]) -> Dict[str, Any]:
        """随机算法"""
        # 基于权重的随机选择
        weights = [instance.get("weight", 1) for instance in instances]
        total_weight = sum(weights)
        
        if total_weight > 0:
            r = random.uniform(0, total_weight)
            upto = 0
            for i, w in enumerate(weights):
                if upto + w >= r:
                    return instances[i]
                upto += w
        
        return random.choice(instances)
    
    def _least_connections(self, instances: List[Dict[str, Any]]) -> Dict[str, Any]:
        """最少连接算法"""
        # 选择连接数最少的实例
        min_connections = min(instance.get("connections", 0) for instance in instances)
        least_connections_instances = [
            instance for instance in instances 
            if instance.get("connections", 0) == min_connections
        ]
        
        # 如果有多个连接数相同的实例，根据响应时间选择
        if len(least_connections_instances) > 1:
            return min(least_connections_instances, key=lambda inst: inst.get("response_time", 1.0))
        
        return least_connections_instances[0]
    
    def _response_time_aware(self, instances: List[Dict[str, Any]]) -> Dict[str, Any]:
        """响应时间感知算法"""
        # 选择响应时间最短的实例
        min_response_time = min(instance.get("response_time", 1.0) for instance in instances)
        fast_instances = [
            instance for instance in instances 
            if instance.get("response_time", 1.0) == min_response_time
        ]
        
        # 如果有多个响应时间相同的实例，根据连接数选择
        if len(fast_instances) > 1:
            return min(fast_instances, key=lambda inst: inst.get("connections", 0))
        
        return fast_instances[0]
    
    def _activity_aware(self, instances: List[Dict[str, Any]]) -> Dict[str, Any]:
        """活跃度感知算法，综合考虑多个指标"""
        # 计算每个实例的活跃度分数
        activity_scores = []
        
        for instance in instances:
            # 基础分数（健康状态）
            base_score = 1 if instance.get("healthy", True) else 0
            
            # 连接数因子（连接数越少，因子越高）
            connections = instance.get("connections", 0)
            conn_factor = 1.0 / (connections + 1)
            
            # 响应时间因子（响应时间越短，因子越高）
            response_time = instance.get("response_time", 1.0)
            resp_factor = 1.0 / (response_time + 0.1)  # 添加一个小值以避免除零
            
            # 错误率因子（错误率越低，因子越高）
            error_rate = instance.get("error_rate", 0)
            error_factor = 1.0 - min(error_rate, 0.99)  # 限制最大错误率为0.99
            
            # 权重因子
            weight = instance.get("weight", 1)
            
            # 计算综合分数
            score = base_score * conn_factor * resp_factor * error_factor * weight
            activity_scores.append((instance, score))
        
        # 按分数排序，选择分数最高的实例
        activity_scores.sort(key=lambda x: x[1], reverse=True)
        
        # 考虑权重和随机性
        total_score = sum(score for _, score in activity_scores)
        if total_score > 0:
            r = random.uniform(0, total_score)
            upto = 0
            for instance, score in activity_scores:
                if upto + score >= r:
                    return instance
                upto += score
        
        return activity_scores[0][0]


class ServiceExecutor:
    """智能服务执行器，执行选定的服务并提供高级功能"""
    
    def __init__(self, max_concurrent: int = 20, load_balancing_algorithm: str = "adaptive"):
        self.registry: ServiceRegistry = service_registry
        self.semaphore = asyncio.Semaphore(max_concurrent)  # 限制并发数，可配置
        self.sync_semaphore = asyncio.Semaphore(max_concurrent)  # 同步执行的并发控制
        self.load_balancer = LoadBalancer(load_balancing_algorithm)  # 使用配置的负载均衡算法
        self.failover_enabled = True  # 启用故障转移
        self.execution_metrics = defaultdict(lambda: {
            "total_calls": 0,
            "success_calls": 0,
            "failed_calls": 0,
            "total_time": 0,
            "avg_time": 0
        })  # 执行指标统计
        self.logger = logging.getLogger(__name__)  # 添加日志记录器
        # 初始化负载均衡器的服务实例
        self._initialize_load_balancer_instances()
    
    async def execute_service(self, service_name: str, parameters: Dict, 
                             timeout: Optional[int] = None, retries: int = 2) -> Any:
        """
        执行指定的服务，支持超时、重试、负载均衡和故障转移
        
        Args:
            service_name: 服务名称
            parameters: 服务参数
            timeout: 超时时间（秒）
            retries: 重试次数
            
        Returns:
            服务执行结果
        """
        # 记录服务调用开始
        self.logger.info(f"开始执行服务: {service_name}")
        
        if not self.registry.service_exists(service_name):
            error_msg = f"未知服务: {service_name}"
            self.logger.error(error_msg)
            raise ValueError(error_msg)
        
        # 检查服务状态
        service_status = self.registry.get_service_status(service_name)
        if service_status == ServiceStatus.INACTIVE:
            # 尝试故障转移
            if self.failover_enabled:
                self.logger.warning(f"服务 '{service_name}' 处于非活动状态，尝试故障转移")
                failover_result = await self._try_failover(service_name, parameters, timeout, retries)
                if failover_result is not None:
                    # 确保结果已完全解析
                    failover_result = await resolve_awaitable(failover_result)
                    
                    # 确保返回结果格式统一
                    if not isinstance(failover_result, dict):
                        failover_result = self._format_result(failover_result, service_name)
                    
                    self.logger.info(f"服务 '{service_name}' 故障转移成功")
                    return failover_result
            
            error_msg = f"服务 '{service_name}' 当前不可用"
            self.logger.error(error_msg)
            raise ValueError(error_msg)
        
        service_info = self.registry.get_service_info(service_name)
        if not service_info:
            error_msg = f"服务信息获取失败: {service_name}"
            self.logger.error(error_msg)
            raise ValueError(error_msg)
        
        service_func = service_info['function']
        
        # 更新服务实例信息（如果有）
        self._update_service_instances(service_name)
        
        # 添加重试逻辑，使用增强的指数退避加抖动
        for attempt in range(retries):
            start_time = time.time()
            instance_id = None
            
            try:
                async with self.semaphore:  # 限制并发
                    # 验证参数
                    validated_params = self._validate_parameters(parameters, service_info.get('parameters', {}))
                    
                    # 检查服务依赖
                    if not await self._check_and_resolve_dependencies(service_name):
                        raise ValueError(f"无法解析服务 '{service_name}' 的依赖")
                    
                    # 使用负载均衡器选择服务实例
                    instance = self.load_balancer.get_instance(service_name)
                    if instance:
                        instance_id = instance.get("instance_id")
                        # 更新实例连接数（开始处理请求）
                        self.load_balancer.update_instance_status(
                            service_name, instance_id, connections=instance.get("connections", 0) + 1
                        )
                    
                    # 执行服务（支持超时控制和协程对象检测）
                    try:
                        # 首先检查service_func是否已经是一个协程对象而不是函数
                        if inspect.iscoroutine(service_func) or inspect.isawaitable(service_func):
                            # 如果它已经是一个协程对象，直接await它
                            self.logger.info(f"服务 '{service_name}' 的service_func已经是一个协程对象，直接处理")
                            if timeout:
                                result = await asyncio.wait_for(service_func, timeout=timeout)
                            else:
                                result = await service_func
                        else:
                            # 然后检查service_func是否是协程函数
                            if timeout:
                                # 使用超时控制执行服务
                                if inspect.iscoroutinefunction(service_func):
                                    result = await asyncio.wait_for(
                                        service_func(**validated_params), 
                                        timeout=timeout
                                    )
                                else:
                                    # 对于同步函数，使用线程池执行器避免阻塞
                                    loop = asyncio.get_event_loop()
                                    result = await asyncio.wait_for(
                                        loop.run_in_executor(None, lambda: service_func(**validated_params)),
                                        timeout=timeout
                                    )
                            else:
                                # 无超时限制执行
                                if inspect.iscoroutinefunction(service_func):
                                    result = await service_func(**validated_params)
                                else:
                                    loop = asyncio.get_event_loop()
                                    result = await loop.run_in_executor(None, lambda: service_func(**validated_params))
                    except asyncio.TimeoutError:
                        raise TimeoutError(f"服务 '{service_name}' 执行超时")
                    except Exception as e:
                        self.logger.error(f"服务 '{service_name}' 执行失败: {str(e)}")
                        raise
                    
                    execution_time = time.time() - start_time
                    
                    # 更新实例连接数和响应时间（完成处理请求）
                    if instance_id:
                        self.load_balancer.update_instance_status(
                            service_name, instance_id, 
                            connections=instance.get("connections", 0) - 1, 
                            response_time=execution_time
                        )
                    
                    # 记录成功执行指标
                    self.registry.record_service_execution(service_name, execution_time, True)
                    self._update_execution_metrics(service_name, execution_time, True)
                    
                    # 确保结果已完全解析
                    result = await resolve_awaitable(result)
                    
                    # 格式化结果
                    if not isinstance(result, dict):
                        formatted_result = self._format_result(result, service_name)
                    else:
                        formatted_result = result
                    
                    # 再次确保结果已完全解析
                    formatted_result = await resolve_awaitable(formatted_result)
                    
                    self.logger.info(f"服务 '{service_name}' 执行成功，耗时: {execution_time:.2f}秒")
                    return formatted_result
                    
            except Exception as e:
                execution_time = time.time() - start_time
                
                # 更新实例状态（记录错误）
                if instance_id:
                    self.load_balancer.update_instance_status(
                        service_name, instance_id, 
                        connections=instance.get("connections", 0) - 1 if instance else 0,
                        response_time=execution_time, 
                        error=True
                    )
                
                # 记录失败执行指标
                self.registry.record_service_execution(service_name, execution_time, False)
                self._update_execution_metrics(service_name, execution_time, False)
                
                if attempt == retries - 1:
                    # 最后一次尝试失败，尝试故障转移
                    if self.failover_enabled:
                        self.logger.warning(f"服务 '{service_name}' 执行失败，尝试故障转移: {str(e)}")
                        failover_result = await self._try_failover(service_name, parameters, timeout, retries)
                        if failover_result is not None:
                            # 确保结果已完全解析
                            failover_result = await resolve_awaitable(failover_result)
                            
                            # 确保返回结果格式统一
                            if not isinstance(failover_result, dict):
                                failover_result = self._format_result(failover_result, service_name)
                            
                            self.logger.info(f"服务 '{service_name}' 故障转移成功")
                            return failover_result
                    
                    # 没有故障转移或故障转移失败，返回错误处理结果
                    error_response = self._handle_error(e, service_name, parameters)
                    self.logger.error(f"服务 '{service_name}' 执行失败: {str(e)}")
                    return error_response
                
                # 指数退避加抖动，根据错误类型调整退避策略
                backoff_time = self._calculate_backoff_time(attempt, e)
                self.logger.warning(f"服务 '{service_name}' 尝试 {attempt + 1}/{retries} 失败: {str(e)}, 将在 {backoff_time:.2f}秒后重试")
                await asyncio.sleep(backoff_time)
    
    async def _try_failover(self, service_name: str, parameters: Dict, 
                           timeout: Optional[int], retries: int) -> Optional[Any]:
        """
        智能故障转移机制，支持多级故障转移策略
        
        Args:
            service_name: 服务名称
            parameters: 服务参数
            timeout: 超时时间
            retries: 重试次数
            
        Returns:
            故障转移成功返回结果，否则返回None
        """
        # 故障转移策略优先级：
        # 1. 同类型备用服务（预先配置）
        # 2. 功能相似服务
        # 3. 降级服务（提供基本功能）
        
        # 第1级：查找预先配置的备用服务
        primary_fallback = self.registry.find_fallback_services(service_name)
        
        # 第2级：查找功能相似的服务
        secondary_fallback = []
        if not primary_fallback:
            self.logger.info(f"没有为服务 '{service_name}' 配置的备用服务，尝试查找相似服务")
            secondary_fallback = self.registry.find_similar_services(service_name)
        
        # 第3级：查找降级服务
        tertiary_fallback = []
        if not primary_fallback and not secondary_fallback:
            self.logger.info(f"没有找到服务 '{service_name}' 的相似服务，尝试查找降级服务")
            tertiary_fallback = self.registry.find_degraded_services(service_name)
        
        # 合并并去重所有可能的备用服务
        all_fallback_services = list(set(primary_fallback + secondary_fallback + tertiary_fallback))
        
        # 对备用服务进行优先级排序
        fallback_priority = self._prioritize_fallback_services(service_name, all_fallback_services)
        
        # 尝试使用备用服务，最多尝试3个
        for fallback_service in fallback_priority[:3]:
            try:
                # 为备用服务调整参数（如果需要）
                adjusted_params = self._adjust_parameters_for_fallback(service_name, fallback_service, parameters)
                
                # 使用较少的重试次数尝试备用服务
                result = await self.execute_service(
                    fallback_service, adjusted_params, timeout, retries=1
                )
                
                # 确保结果已解析
                result = await resolve_awaitable(result)
                
                # 如果结果是成功的，添加故障转移标识并返回
                if isinstance(result, dict) and result.get("success", False):
                    result["failover"] = True
                    result["original_service"] = service_name
                    result["fallback_service"] = fallback_service
                    result["fallback_level"] = 1 if fallback_service in primary_fallback else 2 if fallback_service in secondary_fallback else 3
                    return result
                
                # 记录备用服务尝试结果
                self.logger.info(f"尝试故障转移到服务 '{fallback_service}' 返回非成功结果")
            except Exception as e:
                # 记录错误但继续尝试下一个备用服务
                self.logger.warning(f"故障转移到服务 '{fallback_service}' 失败: {str(e)}")
                
        # 所有备用服务都失败
        self.logger.error(f"服务 '{service_name}' 所有故障转移尝试都失败")
        return None
    
    async def _check_and_resolve_dependencies(self, service_name: str) -> bool:
        """
        检查并解析服务依赖，支持依赖服务的故障转移
        
        Args:
            service_name: 服务名称
            
        Returns:
            所有依赖是否成功解析
        """
        try:
            # 获取服务依赖
            dependencies = self.registry.get_service_dependencies(service_name)
            
            for dep_name in dependencies:
                # 检查依赖服务状态
                dep_status = self.registry.get_service_status(dep_name)
                
                if dep_status != ServiceStatus.ACTIVE:
                    self.logger.warning(f"依赖服务 '{dep_name}' 不可用，尝试故障转移")
                    
                    # 尝试故障转移依赖服务
                    if not await self._try_failover_dependency(dep_name):
                        self.logger.error(f"无法故障转移依赖服务 '{dep_name}'")
                        return False
            
            return True
        except Exception as e:
            self.logger.error(f"解析服务 '{service_name}' 的依赖时出错: {str(e)}")
            return False
    
    async def _try_failover_dependency(self, dep_name: str) -> bool:
        """
        尝试故障转移依赖服务
        
        Args:
            dep_name: 依赖服务名称
            
        Returns:
            是否故障转移成功
        """
        try:
            # 查找依赖服务的备用服务
            fallback_deps = self.registry.find_fallback_services(dep_name)
            
            for fallback_dep in fallback_deps:
                # 检查备用依赖服务是否可用
                if self.registry.service_exists(fallback_dep):
                    dep_status = self.registry.get_service_status(fallback_dep)
                    if dep_status == ServiceStatus.ACTIVE:
                        # 注册临时的依赖映射关系
                        self.registry.register_temporary_dependency_mapping(dep_name, fallback_dep)
                        self.logger.info(f"成功故障转移依赖服务: {dep_name} -> {fallback_dep}")
                        return True
            
            # 如果没有找到合适的备用依赖服务，尝试查找相似服务
            self.logger.warning(f"没有找到依赖服务 '{dep_name}' 的备用服务，尝试查找相似服务")
            similar_services = self.registry.find_similar_services(dep_name)
            
            for similar_service in similar_services:
                if self.registry.service_exists(similar_service):
                    service_status = self.registry.get_service_status(similar_service)
                    if service_status == ServiceStatus.ACTIVE:
                        self.registry.register_temporary_dependency_mapping(dep_name, similar_service)
                        self.logger.info(f"成功使用相似服务作为依赖: {dep_name} -> {similar_service}")
                        return True
            
            # 如果没有找到合适的备用依赖服务，记录日志
            self.logger.warning(f"没有可用的依赖服务 '{dep_name}' 的备用服务")
            return False
        except Exception as e:
            self.logger.error(f"依赖服务故障转移过程中出错: {str(e)}")
            return False
    
    def execute_service_sync(self, service_name: str, parameters: Dict, timeout: Optional[int] = None, retries: int = 2) -> Any:
        """
        同步执行服务（用于非异步环境）
        
        Args:
            service_name: 服务名称
            parameters: 服务参数
            timeout: 超时时间（秒）
            retries: 重试次数
            
        Returns:
            服务执行结果
        """
        import concurrent.futures
        
        # 记录服务调用开始
        self.logger.info(f"同步开始执行服务: {service_name}")
        
        if not self.registry.service_exists(service_name):
            error_msg = f"未知服务: {service_name}"
            self.logger.error(error_msg)
            raise ValueError(error_msg)
        
        # 检查服务状态
        service_status = self.registry.get_service_status(service_name)
        if service_status == ServiceStatus.INACTIVE:
            error_msg = f"服务 '{service_name}' 当前不可用"
            self.logger.error(error_msg)
            raise ValueError(error_msg)
        
        service_info = self.registry.get_service_info(service_name)
        if not service_info:
            error_msg = f"服务信息获取失败: {service_name}"
            self.logger.error(error_msg)
            raise ValueError(error_msg)
        
        service_func = service_info['function']
        
        # 更新服务实例信息（如果有）
        self._update_service_instances(service_name)
        
        # 添加重试逻辑，使用增强的指数退避加抖动
        for attempt in range(retries):
            start_time = time.time()
            try:
                # 检查服务依赖
                if not self._check_dependencies_sync(service_name):
                    raise ValueError(f"无法解析服务 '{service_name}' 的依赖")
                
                # 验证参数
                validated_params = self._validate_parameters(parameters, service_info.get('parameters', {}))
                
                # 终极解决方案：检查service_func的状态并处理所有可能的情况
                try:
                    # 首先检查service_func是否已经是一个协程对象而不是函数
                    if inspect.iscoroutine(service_func) or inspect.isawaitable(service_func):
                        # 如果它已经是一个协程对象，记录错误并返回错误信息
                        logger.error(f"服务 '{service_name}' 的service_func已经是一个协程对象，无法再次调用")
                        raise TypeError(f"服务 '{service_name}' 的service_func已经是一个协程对象，无法再次调用")
                    
                    # 然后检查service_func是否是协程函数
                    if inspect.iscoroutinefunction(service_func):
                        # 如果是协程函数，直接使用run_in_async_context执行
                        if timeout:
                            # 包装协程函数，添加超时控制
                            async def coro_with_timeout():
                                try:
                                    return await asyncio.wait_for(service_func(**validated_params), timeout=timeout)
                                except asyncio.TimeoutError:
                                    raise TimeoutError(f"服务 '{service_name}' 执行超时")
                            result = run_in_async_context(coro_with_timeout)
                        else:
                            result = run_in_async_context(service_func, **validated_params)
                    else:
                        # 对于同步函数，使用ThreadPoolExecutor执行
                        # 但我们需要确保它不是协程对象
                        def safe_service_call():
                            # 再次检查，以防在执行前service_func变成了协程对象
                            if inspect.iscoroutine(service_func) or inspect.isawaitable(service_func):
                                raise TypeError(f"执行前，service_func变成了协程对象")
                            return service_func(**validated_params)
                        
                        if timeout:
                            with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
                                future = executor.submit(safe_service_call)
                                try:
                                    result = future.result(timeout=timeout)
                                except concurrent.futures.TimeoutError:
                                    raise TimeoutError(f"服务 '{service_name}' 执行超时")
                        else:
                            with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
                                future = executor.submit(safe_service_call)
                                result = future.result()
                except TimeoutError:
                    raise
                except Exception as e:
                    logger.error(f"服务 '{service_name}' 执行失败: {str(e)}")
                    raise
                
                execution_time = time.time() - start_time
                # 记录成功执行指标
                self.registry.record_service_execution(service_name, execution_time, True)
                self._update_execution_metrics(service_name, execution_time, True)
                
                # 使用辅助函数处理协程和awaitable对象
                try:
                    if inspect.iscoroutine(result) or inspect.isawaitable(result):
                        # 对于协程对象或可等待对象，使用resolve_awaitable函数在异步上下文中解析
                        result = run_in_async_context(resolve_awaitable, result)
                    elif inspect.iscoroutinefunction(result):
                        # 对于协程函数，调用它获取协程对象并使用resolve_awaitable解析
                        coro_obj = result()
                        result = run_in_async_context(resolve_awaitable, coro_obj)
                except Exception as e:
                    self.logger.error(f"在异步上下文中解析结果时出错: {str(e)}")
                    return self._handle_error(e, service_name, parameters)
                
                formatted_result = self._format_result(result, service_name)
                self.logger.info(f"同步服务 '{service_name}' 执行成功，耗时: {execution_time:.2f}秒")
                return formatted_result
                
            except Exception as e:
                execution_time = time.time() - start_time
                # 记录失败执行指标
                self.registry.record_service_execution(service_name, execution_time, False)
                self._update_execution_metrics(service_name, execution_time, False)
                
                if attempt == retries - 1:
                    # 最后一次尝试失败，返回错误处理结果
                    error_response = self._handle_error(e, service_name, parameters)
                    self.logger.error(f"同步服务 '{service_name}' 执行失败: {str(e)}")
                    return error_response
                
                # 指数退避加抖动
                backoff_time = self._calculate_backoff_time(attempt, e)
                self.logger.warning(f"同步服务 '{service_name}' 尝试 {attempt + 1}/{retries} 失败: {str(e)}, 将在 {backoff_time:.2f}秒后重试")
                time.sleep(backoff_time)
    
    def _get_service_timeout(self, service_name: str) -> int:
        """
        获取服务的超时时间，基于服务类型和历史执行数据
        
        Args:
            service_name: 服务名称
            
        Returns:
            超时时间（秒）
        """
        # 从服务注册表获取配置的超时时间
        configured_timeout = self.registry.get_service_timeout(service_name)
        if configured_timeout is not None:
            return configured_timeout
        
        # 根据服务类型设置不同的默认超时时间
        service_timeouts = {
            "research_assistant": 300,  # 5分钟
            "knowledge_graph": 120,     # 2分钟
            "user_management": 60,      # 1分钟
            "ticket_system": 60,        # 1分钟
            "default": 180              # 3分钟
        }
        
        # 基于历史执行数据动态调整超时时间
        default_timeout = service_timeouts.get(service_name, service_timeouts["default"])
        metrics = self.execution_metrics.get(service_name, {})
        
        if metrics.get("total_calls", 0) > 10:  # 至少有10次调用才动态调整
            avg_time = metrics.get("avg_time", default_timeout)
            # 超时时间设置为平均执行时间的3倍，但不超过默认超时的2倍
            dynamic_timeout = min(avg_time * 3, default_timeout * 2)
            return max(int(dynamic_timeout), 10)  # 确保至少10秒
        
        return default_timeout
    
    def _validate_parameters(self, provided_params: Dict, expected_params: Dict) -> Dict:
        """
        增强的参数验证和清理机制
        
        Args:
            provided_params: 提供的参数
            expected_params: 期望的参数
            
        Returns:
            验证后的参数
        """
        validated_params = {}
        missing_required_params = []
        validation_warnings = []
        
        # 处理空参数
        if not provided_params:
            provided_params = {}
        
        # 验证每个期望的参数
        for param_name, param_desc in expected_params.items():
            param_value = None
            
            # 检查参数是否提供且不为空
            if param_name in provided_params and provided_params[param_name] not in [None, ""]:
                param_value = provided_params[param_name]
                
                # 参数类型验证和转换
                if isinstance(param_desc, dict):
                    param_type = param_desc.get('type')
                    if param_type:
                        try:
                            # 尝试转换参数类型
                            param_value = self._convert_param_type(param_value, param_type)
                        except (ValueError, TypeError) as e:
                            validation_warnings.append(f"参数 '{param_name}' 类型转换失败: {str(e)}")
                
                # 参数值范围验证
                if isinstance(param_desc, dict):
                    min_value = param_desc.get('min')
                    max_value = param_desc.get('max')
                    if min_value is not None and param_value < min_value:
                        validation_warnings.append(f"参数 '{param_name}' 值 {param_value} 小于最小值 {min_value}")
                    if max_value is not None and param_value > max_value:
                        validation_warnings.append(f"参数 '{param_name}' 值 {param_value} 大于最大值 {max_value}")
                
                validated_params[param_name] = param_value
            else:
                # 参数不存在或为空，根据参数描述提供默认值
                default_value = self._get_default_value(param_desc)
                validated_params[param_name] = default_value
                
                # 检查是否为必需参数
                if isinstance(param_desc, dict) and param_desc.get('required', False):
                    missing_required_params.append(param_name)
        
        # 处理额外的参数（如果允许）
        for param_name, param_value in provided_params.items():
            if param_name not in expected_params:
                # 检查是否允许额外参数
                allow_extra = isinstance(expected_params, dict) and expected_params.get('allow_extra', False)
                if allow_extra:
                    validated_params[param_name] = param_value
                else:
                    validation_warnings.append(f"忽略未知参数: {param_name}")
        
        # 记录验证警告
        for warning in validation_warnings:
            self.logger.warning(warning)
        
        # 如果有缺失的必需参数，抛出异常
        if missing_required_params:
            raise ValueError(f"缺少必需参数: {', '.join(missing_required_params)}")
            
        return validated_params
    
    def _convert_param_type(self, value: Any, target_type: str) -> Any:
        """
        根据目标类型转换参数值
        
        Args:
            value: 原始值
            target_type: 目标类型
            
        Returns:
            转换后的值
        """
        if target_type.lower() == 'int':
            return int(float(value))  # 允许从字符串或浮点数转换
        elif target_type.lower() == 'float':
            return float(value)
        elif target_type.lower() == 'bool':
            if isinstance(value, str):
                return value.lower() in ('true', 'yes', '1', 'y')
            return bool(value)
        elif target_type.lower() == 'list':
            if isinstance(value, str):
                # 尝试从逗号分隔的字符串解析列表
                if ',' in value:
                    return [item.strip() for item in value.split(',')]
                # 尝试从JSON字符串解析列表
                try:
                    import json
                    parsed_value = json.loads(value)
                    if isinstance(parsed_value, list):
                        return parsed_value
                except json.JSONDecodeError:
                    pass
            return list(value) if isinstance(value, (list, tuple, set)) else [value]
        elif target_type.lower() == 'dict':
            if isinstance(value, str):
                # 尝试从JSON字符串解析字典
                try:
                    import json
                    return json.loads(value)
                except json.JSONDecodeError:
                    # 尝试从键值对字符串解析字典
                    if '=' in value:
                        result = {}
                        for pair in value.split(','):
                            if '=' in pair:
                                key, val = pair.split('=', 1)
                                result[key.strip()] = val.strip()
                        return result
            return dict(value) if isinstance(value, (dict, list, tuple)) else {}
        
        # 默认返回原始值
        return value
    
    def _get_default_value(self, param_desc) -> Any:
        """
        根据参数描述获取默认值，并支持动态默认值
        
        Args:
            param_desc: 参数描述
            
        Returns:
            默认值
        """
        if isinstance(param_desc, dict):
            default_value = param_desc.get('default')
            
            # 支持动态默认值（如当前时间）
            if isinstance(default_value, str) and default_value.startswith('dynamic:'):
                dynamic_type = default_value[8:].strip()
                if dynamic_type == 'current_time':
                    return datetime.now().isoformat()
                elif dynamic_type == 'current_date':
                    return datetime.now().date().isoformat()
                elif dynamic_type == 'uuid':
                    import uuid
                    return str(uuid.uuid4())
            
            return default_value
        return None
    
    def _format_result(self, result: Any, service_name: str) -> Dict[str, Any]:
        """
        增强的结果格式化，支持不同数据类型和结果元数据
        
        Args:
            result: 服务执行结果
            service_name: 服务名称
            
        Returns:
            格式化后的结果
        """
        # 获取服务信息，用于添加元数据
        service_info = self.registry.get_service_info(service_name) or {}
        version = service_info.get('version', '1.0')
        
        # 准备基础结果
        formatted_result = {
            "success": True,
            "service": service_name,
            "version": version,
            "timestamp": self._get_timestamp(),
            "execution_id": self._generate_execution_id(),
            "data": {}
        }
        
        # 根据结果类型进行不同的格式化
        if isinstance(result, dict):
            # 如果结果已经是字典，检查是否包含标准字段
            if 'success' in result and 'data' in result:
                # 合并标准格式的结果
                formatted_result.update(result)
            else:
                # 将整个结果作为data字段的值
                formatted_result['data'] = result
        elif isinstance(result, (list, tuple)):
            # 对于列表和元组，添加额外的信息
            formatted_result['data'] = {
                "items": list(result),
                "count": len(result)
            }
        elif result is None:
            # 对于None结果，提供默认消息
            formatted_result['data'] = {
                "message": "操作成功，但没有返回数据"
            }
        else:
            # 其他类型的结果包装成字典
            formatted_result['data'] = {"result": result}
        
        # 添加执行指标
        metrics = self.execution_metrics.get(service_name, {})
        if metrics.get("total_calls", 0) > 0:
            formatted_result['metrics'] = {
                "avg_execution_time": round(metrics.get("avg_time", 0), 3),
                "success_rate": round(metrics.get("success_calls", 0) / metrics.get("total_calls", 1) * 100, 2)
            }
        
        return formatted_result
    
    def _handle_error(self, error: Exception, service_name: str, parameters: Dict) -> Dict[str, Any]:
        """
        增强的错误处理机制，支持错误分类和恢复建议
        
        Args:
            error: 错误异常
            service_name: 服务名称
            parameters: 参数
            
        Returns:
            格式化后的错误信息
        """
        error_type = type(error).__name__
        error_message = str(error)
        
        # 根据错误类型分类和提供更友好的错误信息
        error_category = "general"
        recovery_suggestion = "请稍后重试，如果问题持续存在，请联系系统管理员"
        
        if isinstance(error, ValueError):
            error_category = "validation"
            if "未知服务" in error_message:
                recovery_suggestion = "请检查服务名称是否正确"
            elif "缺少必需参数" in error_message:
                recovery_suggestion = "请提供所有必需的参数"
        elif isinstance(error, TimeoutError):
            error_category = "timeout"
            recovery_suggestion = "请求处理时间过长，请尝试简化请求或稍后重试"
        elif isinstance(error, ConnectionError):
            error_category = "connection"
            recovery_suggestion = "网络连接失败，请检查网络设置"
        elif isinstance(error, PermissionError):
            error_category = "permission"
            recovery_suggestion = "权限不足，请检查您的权限设置"
        elif isinstance(error, TypeError):
            error_category = "type"
            recovery_suggestion = "参数类型错误，请检查参数格式"
        elif isinstance(error, KeyError):
            error_category = "key"
            recovery_suggestion = "找不到指定的键，请检查请求参数"
        
        # 根据服务类型提供特定的恢复建议
        if "research" in service_name.lower() and error_category == "timeout":
            recovery_suggestion = "研究任务执行时间过长，请尝试简化问题范围或使用更具体的查询词"
        elif "knowledge_graph" in service_name.lower() and error_category == "key":
            recovery_suggestion = "找不到指定的实体或关系，请检查拼写或尝试其他相关术语"
        
        # 记录错误详情到日志（不包含敏感参数）
        sanitized_params = self._sanitize_parameters(parameters)
        self.logger.error(
            f"服务 '{service_name}' 执行错误 [{error_category}]: {error_message}, "
            f"参数: {sanitized_params}",
            exc_info=True  # 包含堆栈跟踪
        )
        
        # 返回格式化的错误响应
        return {
            "success": False,
            "service": service_name,
            "error": error_message,
            "error_type": error_type,
            "error_category": error_category,
            "recovery_suggestion": recovery_suggestion,
            "parameters": sanitized_params,  # 只返回清理后的参数
            "timestamp": self._get_timestamp(),
            "execution_id": self._generate_execution_id()
        }
    
    def _sanitize_parameters(self, parameters: Dict) -> Dict:
        """
        清理参数，移除敏感信息
        
        Args:
            parameters: 原始参数
            
        Returns:
            清理后的参数
        """
        if not parameters:
            return {}
        
        # 敏感参数列表
        sensitive_keys = {'password', 'token', 'api_key', 'secret', 'auth', 'credentials'}
        
        sanitized = {}
        for key, value in parameters.items():
            # 检查键名是否包含敏感信息
            key_lower = key.lower()
            if any(sensitive in key_lower for sensitive in sensitive_keys):
                sanitized[key] = '******'
            # 检查嵌套字典中的敏感信息
            elif isinstance(value, dict):
                sanitized[key] = self._sanitize_parameters(value)
            # 检查列表中的敏感信息
            elif isinstance(value, (list, tuple)):
                sanitized[key] = [
                    self._sanitize_parameters(item) if isinstance(item, dict) else item
                    for item in value
                ]
            else:
                sanitized[key] = value
        
        return sanitized
    
    def _get_timestamp(self) -> str:
        """获取当前时间戳"""
        return datetime.now().isoformat()
    
    def _generate_execution_id(self) -> str:
        """生成唯一的执行ID"""
        import uuid
        return str(uuid.uuid4())
    
    def _update_execution_metrics(self, service_name: str, execution_time: float, success: bool):
        """
        更新服务执行指标
        
        Args:
            service_name: 服务名称
            execution_time: 执行时间（秒）
            success: 是否成功
        """
        metrics = self.execution_metrics[service_name]
        metrics["total_calls"] += 1
        if success:
            metrics["success_calls"] += 1
        else:
            metrics["failed_calls"] += 1
        metrics["total_time"] += execution_time
        metrics["avg_time"] = metrics["total_time"] / metrics["total_calls"]
    
    def _calculate_backoff_time(self, attempt: int, error: Exception) -> float:
        """
        根据尝试次数和错误类型计算退避时间
        
        Args:
            attempt: 当前尝试次数（从0开始）
            error: 错误异常
            
        Returns:
            退避时间（秒）
        """
        # 基础指数退避
        base_backoff = 2 ** attempt
        
        # 根据错误类型调整退避时间
        error_type = type(error).__name__
        if isinstance(error, TimeoutError):
            # 超时错误，增加退避时间
            base_backoff *= 1.5
        elif isinstance(error, ConnectionError):
            # 连接错误，也增加退避时间
            base_backoff *= 1.3
        
        # 添加抖动（随机因子）
        jitter = random.uniform(0.8, 1.2)
        backoff_time = base_backoff * jitter
        
        # 限制最大退避时间为30秒
        return min(backoff_time, 30)
    
    def _initialize_load_balancer_instances(self):
        """
        初始化负载均衡器的服务实例信息
        """
        try:
            for service_name in self.registry.list_services():
                # 获取服务实例信息
                instances = self.registry.get_service_instances(service_name)
                if instances:
                    self.load_balancer.register_service_instances(service_name, instances)
        except Exception as e:
            self.logger.error(f"初始化负载均衡器实例时出错: {str(e)}")
    
    def _update_service_instances(self, service_name: str):
        """
        更新服务实例信息
        
        Args:
            service_name: 服务名称
        """
        try:
            instances = self.registry.get_service_instances(service_name)
            if instances:
                self.load_balancer.register_service_instances(service_name, instances)
        except Exception as e:
            self.logger.error(f"更新服务实例信息时出错: {str(e)}")
    
    def _check_dependencies_sync(self, service_name: str) -> bool:
        """
        同步检查服务依赖
        
        Args:
            service_name: 服务名称
            
        Returns:
            所有依赖是否可用
        """
        try:
            # 获取服务依赖
            dependencies = self.registry.get_service_dependencies(service_name)
            
            for dep_name in dependencies:
                # 检查依赖服务状态
                dep_status = self.registry.get_service_status(dep_name)
                
                if dep_status != ServiceStatus.ACTIVE:
                    # 尝试同步故障转移依赖服务
                    if not self._try_failover_dependency_sync(dep_name):
                        self.logger.error(f"无法故障转移依赖服务 '{dep_name}'")
                        return False
            
            return True
        except Exception as e:
            self.logger.error(f"同步检查服务 '{service_name}' 的依赖时出错: {str(e)}")
            return False
    
    def _try_failover_dependency_sync(self, dep_name: str) -> bool:
        """
        同步尝试故障转移依赖服务
        
        Args:
            dep_name: 依赖服务名称
            
        Returns:
            是否故障转移成功
        """
        try:
            # 查找依赖服务的备用服务
            fallback_deps = self.registry.find_fallback_services(dep_name)
            
            for fallback_dep in fallback_deps:
                # 检查备用依赖服务是否可用
                if self.registry.service_exists(fallback_dep):
                    dep_status = self.registry.get_service_status(fallback_dep)
                    if dep_status == ServiceStatus.ACTIVE:
                        # 注册临时的依赖映射关系
                        self.registry.register_temporary_dependency_mapping(dep_name, fallback_dep)
                        self.logger.info(f"成功故障转移依赖服务: {dep_name} -> {fallback_dep}")
                        return True
            
            return False
        except Exception as e:
            self.logger.error(f"同步依赖服务故障转移过程中出错: {str(e)}")
            return False
    
    def _prioritize_fallback_services(self, original_service: str, fallback_services: List[str]) -> List[str]:
        """
        对备用服务进行优先级排序
        
        Args:
            original_service: 原始服务名称
            fallback_services: 备用服务列表
            
        Returns:
            排序后的备用服务列表
        """
        # 简单实现：基于服务名称相似度排序
        def calculate_similarity(service1: str, service2: str) -> float:
            """计算两个服务名称的相似度"""
            # 简单的相似度计算，可以替换为更复杂的算法
            from difflib import SequenceMatcher
            return SequenceMatcher(None, service1, service2).ratio()
        
        # 按与原始服务的相似度排序
        return sorted(fallback_services, key=lambda s: calculate_similarity(original_service, s), reverse=True)
    
    def _adjust_parameters_for_fallback(self, original_service: str, fallback_service: str, parameters: Dict) -> Dict:
        """
        为备用服务调整参数
        
        Args:
            original_service: 原始服务名称
            fallback_service: 备用服务名称
            parameters: 原始参数
            
        Returns:
            调整后的参数
        """
        # 简单实现：可以根据服务映射关系进行参数调整
        # 这里可以扩展为更复杂的参数映射逻辑
        return parameters.copy()
    
    def get_execution_metrics(self, service_name: str = None) -> Dict:
        """
        获取服务执行指标
        
        Args:
            service_name: 服务名称，如果为None则获取所有服务的指标
            
        Returns:
            执行指标
        """
        if service_name:
            return self.execution_metrics.get(service_name, {})
        return dict(self.execution_metrics)


# 全局服务执行器实例
service_executor = ServiceExecutor()


def get_service_executor() -> ServiceExecutor:
    """获取服务执行器实例"""
    return service_executor


# 导出
__all__ = ["ServiceExecutor", "service_executor", "get_service_executor"]